package com.red.springmanual.usespring;

import com.alibaba.druid.pool.DruidDataSource;
import javafx.application.Platform;
import org.mybatis.spring.boot.autoconfigure.MybatisLanguageDriverAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import javax.xml.ws.WebEndpoint;

@ComponentScan("com.red.springmanual.usespring")
//@EnableAspectJAutoProxy
@EnableTransactionManagement
@Configuration
//@Import({DataSourceAutoConfiguration.class})
public class AppConfig {

//    @Autowired
//    private DataSource dataSource;

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl("jdbc:h2:mem:test");
        dataSource.setUsername("sa");
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setPassword("");
        return dataSource;
    }


//    @Bean
//    public DataSource dataSource(){
//        DruidDataSource druidDataSource = new DruidDataSource();
//        druidDataSource.setUrl("jdbc:h2:tcp://localhost/~/test");
//        druidDataSource.setUsername("sa");
//        druidDataSource.setDriverClassName("org.h2.Driver");
//        return druidDataSource;
//    }


    @PostConstruct
    public void ddl(){
        jdbcTemplate().execute("create table if not exists TB_USER (\n" +
                "                                       USER_ID int not null primary key auto_increment,\n" +
                "                                       USER_NAME varchar(100)\n" +
                "    )");
    }

    @Bean
    public JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(dataSource());
    }



    @Bean
    public PlatformTransactionManager transactionManager(){
        DataSourceTransactionManager dataSourceTransactionManager  =new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource());
        return dataSourceTransactionManager;
    }

//    @Bean
//    public OrderService orderService1(){
//        return new OrderService();
//    }



   /* @Bean
    public JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public PlatformTransactionManager transactionManager(){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }
*/





}
